package com.aplink.generic.widgets;

import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.SqlDataType;
import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.controlspec.ControlSpec;
import com.aplink.generic.util.convertcount.GenericConverter;
import com.aplink.generic.util.convertcount.GenericFilter;

public class CustomTextview extends BaseView {

    private final TextView mTextView;

    public CustomTextview( GenericFragment dataEntryActivity,
                           ControlSpec controlSpec, View view ) {
        super( dataEntryActivity, controlSpec, view );
        this.mTextView = (TextView) view;
    }

    @Override
    protected void loadDataToView() {
        final String dataObj = getControlSpec().getDataObject();
        String content = null;
        final String mappingName = getControlSpec().getMappingName();
        if ( dataObj != "" ) {
            content = getHashPassedValues().get( mappingName );
        }
        this.mTextView.setText( content );

    }

    @Override
    protected void doIfNotHashPassedValues() {
        return;
    }

    @Override
    protected void setEventListener() {
        this.mTextView.setOnClickListener( getOnViewClickListener() );
    }

    @Override
    protected void initViewProperties() {
        final boolean isVisibility = getControlSpec().isVisibility();
        mTextView.setMovementMethod( new ScrollingMovementMethod() );
        if ( ! isVisibility ) {
            this.mTextView.setVisibility( View.GONE );
        }
        this.mTextView.setTag( getControlSpec() );
    }

    @Override
    public GenericAttribute<?, ?> getAttribute() {
        String name = getControlSpec().getMappingName();
        SqlDataType dataType = GenericFilter
                .getDataTypeOfAttributeFromObjectPropMappingName(
                        getObjPropList(), name );
        String value = mTextView.getText().toString();
        return GenericConverter.getGenericAttribute( name, dataType, value );
    }
}
